package com.matrix.tool.ui.tool

import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.activity.enableEdgeToEdge
import androidx.annotation.ColorInt
import androidx.core.view.isGone
import com.alibaba.android.arouter.facade.annotation.Route
import com.jrummyapps.android.colorpicker.ColorPickerDialog
import com.jrummyapps.android.colorpicker.ColorPickerDialogListener
import com.matrix.basecore.base.BaseActivity
import com.matrix.basecore.constant.Constant
import com.matrix.tool.R
import com.matrix.tool.databinding.ActivityMarqueeBinding

@Route(path = Constant.ARouterPath.MarqueeActivityPath)
class MarqueeActivity : BaseActivity<ActivityMarqueeBinding>() {

    private var isBg: Boolean = true
    override fun getViewBinding(): ActivityMarqueeBinding {
        enableEdgeToEdge()
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
        return ActivityMarqueeBinding.inflate(layoutInflater)
    }

    override fun initView() {
        mViewBinding.vtv.setText("爱心❤弹幕")
        mViewBinding.vtv.startAnimation()
        mViewBinding.bigSeekbar.max = 150
        mViewBinding.speedSeekbar.max = 80
        mViewBinding.bigSeekbar.progress=80
        mViewBinding.speedSeekbar.progress=10
    }

    override fun initListener() {
        mViewBinding.vtv.setOnClickListener {
            mViewBinding.rlMainLayout.let {
                if (it.isGone) {
                    it.visibility = VISIBLE
                    mViewBinding.llSetting.visibility = GONE
                } else {
                    it.visibility = GONE
                }
            }
        }

        mViewBinding.ivBack.setOnClickListener {
            finish()
        }

        mViewBinding.tvSend.setOnClickListener {
            if (mViewBinding.etText.text.isNotEmpty()) {
                mViewBinding.vtv.setText(mViewBinding.etText.text.toString())
            }
        }
        mViewBinding.ivSetting.setOnClickListener {
            if (mViewBinding.llSetting.isGone) {
                mViewBinding.llSetting.visibility = VISIBLE
            } else {
                mViewBinding.llSetting.visibility = GONE
            }
        }
        mViewBinding.ctvbg.setOnClickListener {
            isBg = true
            showColorPicker()
        }
        mViewBinding.ctvspeed.setOnClickListener {
            isBg = false
            showColorPicker()
        }

        mViewBinding.bigSeekbar.setOnSeekBarChangeListener(object :OnSeekBarChangeListener{
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                mViewBinding.vtv.width=(p1)
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }
        })

        mViewBinding.speedSeekbar.setOnSeekBarChangeListener(object :OnSeekBarChangeListener{
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                mViewBinding.vtv.setSpeed(p1)
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }
        })
    }

    private fun showColorPicker() {
        //传入的默认color
        val colorPickerDialog: ColorPickerDialog = ColorPickerDialog.newBuilder()
            .setDialogTitle(R.string.color_select) //设置dialog标题
            .setDialogType(ColorPickerDialog.TYPE_PRESETS) //设置为自定义模式
            .setShowAlphaSlider(false) //设置有透明度模式，默认没有透明度
            .setDialogId(80) //设置Id,回调时传回用于判断
            .setAllowPresets(false) //不显示预知模式
            .create()
        //Buider创建
        colorPickerDialog.setColorPickerDialogListener(pickerDialogListener)
        //设置回调，用于获取选择的颜色
        colorPickerDialog.show(this.getFragmentManager(), "color-picker-dialog")
    }

    private val pickerDialogListener: ColorPickerDialogListener =
        object : ColorPickerDialogListener {
            override fun onColorSelected(dialogId: Int, @ColorInt color: Int) {
                if (isBg) {
                    mViewBinding.ctvbg.setBackColor(color)
                    mViewBinding.main.setBackgroundColor(color)
                } else {
                    mViewBinding.ctvspeed.setBackColor(color)
                    mViewBinding.vtv.setColor(color)
                }
            }

            override fun onDialogDismissed(dialogId: Int) {

            }
        }

    override fun onDestroy() {
        super.onDestroy()
        mViewBinding.vtv.stopAnimation()
    }
}
